﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Moonlite.Platform.General;
using Moonlite.Platform.Text.File;
namespace Moonlite.UI.Shell.Controls {
    public partial class TemplateManagerForm : Form {
        private List<string> SessionDeletedTemplates { get; set; }
        private TemplateCollection DummyTemplates { get; set; }
        private bool Accepted { get; set; }
        public void PopulateGrid() {
            gridControl.DataSource = TemplateParser.GetAllTemplates();
        }

        private void RemoveDummyTemplates() {
            foreach (Template template in DummyTemplates) {
                try {
                    File.Delete(template.FileName);
                }
                catch (IOException exception) {
                    Logger.Log += exception.Message + Environment.NewLine;
                }
            }
        }

        private void OnOKClick(object sender, EventArgs e) {
            Accepted = true;
            SessionDeletedTemplates.ForEach(filePath => {
                try {
                    File.Delete(filePath);
                }
                catch (IOException exception) {
                    Logger.Log += exception.Message + Environment.NewLine;
                }
            });

            PopulateGrid();
            Close();
        }

        private void OnCancelClick(object sender, EventArgs e) {
            Accepted = true;
            RemoveDummyTemplates();
            Close();
        }

        private void OnBrowseClick(object sender, EventArgs e) {
            using (OpenFileDialog openFileDialog = new OpenFileDialog { Filter = "Moonlite Template Files (*.mtp)|*.mtp" }) {
                if (openFileDialog.ShowDialog() != DialogResult.OK) {
                    return;
                }

                string targetFileName = String.Format("{0}\\Templates\\{1}", Application.StartupPath, Path.GetFileName(openFileDialog.FileName));
                File.Copy(openFileDialog.FileName, targetFileName);
                DummyTemplates.Add(TemplateParser.ParseTemplateHeader(targetFileName));
                PopulateGrid();
            }
        }

        private void OnGridControlKeyUp(object sender, KeyEventArgs e) {
            const string DeleteString = " (Will be deleted upon pressing OK)";
            if (gridControl.SelectedRows == null || gridControl.SelectedRows.Count == 0) {
                return;
            }

            if (e.KeyCode != Keys.Delete) {
                return;
            }
            if (((string)gridControl.SelectedRows[0].Cells["Name"].Value).IndexOf(DeleteString) > -1) {
                return;
            }

            SessionDeletedTemplates.Add(gridControl.SelectedRows[0].Cells["Path"].Value as string);
            gridControl.SelectedRows[0].Cells["Name"].Value += DeleteString;
        }

        private void OnFormClosed(object sender, FormClosedEventArgs e) {
            if (Accepted == false) {
                RemoveDummyTemplates();
            }
        }

        public TemplateManagerForm() {
            InitializeComponent();
            PopulateGrid();
            SessionDeletedTemplates = new List<string>();
            DummyTemplates = new TemplateCollection();
        }
    }
}